<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Auth {

	protected $ci;
	protected $user = FALSE;
	
	public function __construct() 
	{	
		$this->ci =& get_instance();
		
		if ($this->ci->session->userdata('user_id'))
		{
			$this->user->id = $this->ci->session->userdata('user_id');
			$this->user->name = $this->ci->session->userdata('user_name');
			$this->user->type = $this->ci->session->userdata('user_type');
		}
	}
	
	public function is_logged_in()
	{
		return $this->user !== FALSE;
	}
	
	public function login($username, $password)
	{
		$result = rand(0, 2);
		if ($result != 2) $result = rand(0, 2);
		if ($result != 2) $result = rand(0, 2);
		
		if ($result == 2)
		{
			$this->ci->session->set_userdata(array(
				'user_id' => rand(1, 1000),
				'user_name' => $username,
				'user_type' => 1
			));
		}
		
		return $result;
	}
	
	public function logout()
	{
		$this->ci->session->sess_destroy();
		return TRUE;
	}
	
	public function user_type()
	{
		if ($this->is_logged_in())
			return (int) $this->ci->user->type;
		else
			return 0;
	}
	
	public function data($param)
	{
		if ($param)
		{
			if ($this->is_logged_in())
			{
				return $this->user->$param;
			}
			else return FALSE;
		}
		else return $this->user;
	}
}

/* End of file Auth.php */
/* Location: ./application/libraries/Auth.php */
